package com.longxi.tianjintianbao.ui.base;

import android.app.Dialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.RelativeSizeSpan;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.utils.DrawableUtils;
import com.longxi.tianjintianbao.widget.CustomDialog;
import com.longxi.tianjintianbao.widget.EmptyLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by cxw on 2018/2/13.
 */

public abstract class BaseActivity<V, T extends BasePresenter<V>> extends AppCompatActivity implements IBaseView, EmptyLayout.OnRetryListener {
    /**
     * 把 EmptyLayout 放在基类统一处理，@Nullable 表明 View 可以为 null，详细可看 ButterKnife
     */
    @Nullable
    @BindView(R.id.tvLeft)
    protected TextView mTvLeft;
    @Nullable
    @BindView(R.id.ivLeft)
    protected ImageView mIvLeft;
    @Nullable
    @BindView(R.id.tvBarTitle)
    protected TextView mTvBarTitle;
    @Nullable
    @BindView(R.id.tvRight)
    public TextView mTvRight;
    @Nullable
    @BindView(R.id.ivRight)
    protected ImageView mIvRight;
    @Nullable
    @BindView(R.id.ivRight2)
    protected ImageView mIvRight2;

    @Nullable
    @BindView(R.id.empty_layout)
    protected EmptyLayout mEmptyLayout;
    @Nullable
    @BindView(R.id.refreshLayout)
    protected SmartRefreshLayout mRefreshLayout;
    @Nullable
    @BindView(R.id.rvContent)
    protected RecyclerView mRvContent;

    protected T mPresenter;
    private CustomDialog mDialogWaiting;

    //得到当前界面的布局文件id(由子类实现)
    protected abstract int provideContentViewId();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        init();

        //判断是否使用MVP模式
        mPresenter = createPresenter();
        if (mPresenter != null) {
            mPresenter.attachView((V) this);//因为之后所有的子类都要实现对应的View接口
        }

        //子类不再需要设置布局ID，也不再需要使用ButterKnife.bind()65
        setContentView(provideContentViewId());
        ButterKnife.bind(this);

        initView();
        initData();
        initListener();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
            mPresenter = null;
        }
    }

    //在setContentView()调用之前调用，可以设置WindowFeature(如：this.requestWindowFeature(Window.FEATURE_NO_TITLE);)
    public void init() {
    }

    public void initView() {
    }

    public void initData() {
    }

    public void initListener() {
    }

    //用于创建Presenter和判断是否使用MVP模式(由子类实现)
    protected abstract T createPresenter();

    /**
     * 显示等待提示框
     */
    public Dialog showWaitingDialog(String tip) {
        hideWaitingDialog();
        View view = View.inflate(this, R.layout.dialog_waiting, null);
        if (!TextUtils.isEmpty(tip))
            ((TextView) view.findViewById(R.id.tvTip)).setText(tip);
        mDialogWaiting = new CustomDialog(this, view, R.style.MyDialog);
        mDialogWaiting.show();
//        mDialogWaiting.setCancelable(false);
        return mDialogWaiting;
    }

    /**
     * 大个的显示等待提示框
     */
    public Dialog showWaitingDialog2(String tip) {
        hideWaitingDialog();
        View view = View.inflate(this, R.layout.dialog_waiting2, null);
        if (!TextUtils.isEmpty(tip))
            ((TextView) view.findViewById(R.id.tvTip)).setText(tip);
        mDialogWaiting = new CustomDialog(this, view, R.style.MyDialog);
        mDialogWaiting.show();
        mDialogWaiting.setCancelable(false);
        return mDialogWaiting;
    }

    /**
     * 隐藏等待提示框
     */
    public void hideWaitingDialog() {
        if (mDialogWaiting != null) {
            mDialogWaiting.dismiss();
            mDialogWaiting = null;
        }
    }

    public void jumpToActivity(Class activity) {
        Intent intent = new Intent(this, activity);
        startActivity(intent);
    }

    public void jumpToActivityForResult(Class activity, int requestCode) {
        Intent intent = new Intent(this, activity);
        startActivityForResult(intent, requestCode);
    }

    /**
     * 跳转并清空activity
     * @param activity
     */
    public void jumpToActivityAndClearTask(Class activity) {
        Intent intent = new Intent(this, activity);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        finish();
    }

    @Override
    public void showLoading() {
        if (mEmptyLayout != null) {
            mEmptyLayout.setEmptyStatus(EmptyLayout.STATUS_LOADING);
        }
    }

    @Override
    public void hideLoading() {
        if (mEmptyLayout != null) {
            mEmptyLayout.hide();
        }
    }

    @Override
    public void showNetError() {
        if (mEmptyLayout != null) {
            mEmptyLayout.setEmptyStatus(EmptyLayout.STATUS_NO_NET);
            mEmptyLayout.setRetryListener(this);
        }
    }

    @Override
    public void showNoData() {
        if (mEmptyLayout != null) {
            mEmptyLayout.setEmptyStatus(EmptyLayout.STATUS_NO_DATA);
        }
    }

    @Override
    public void showError() {
        if (mEmptyLayout != null) {
            mEmptyLayout.setEmptyStatus(EmptyLayout.STATUS_ERROR);
        }
    }

    @Override
    public void onRetry() {

    }

    @Override
    public void finishRefresh() {
        if (mRefreshLayout != null) {
            mRefreshLayout.finishRefresh();
        }
    }

    @Override
    public void finishLoadMore() {
        if (mRefreshLayout != null) {
            mRefreshLayout.finishLoadMore();
        }
    }

    @Override
    public void setTitle(String title) {
        if(mTvBarTitle != null){
            mTvBarTitle.setVisibility(View.VISIBLE);
            mTvBarTitle.setText(title);
        }
    }

    @Override
    public void setTvLeft(String name) {
        if(mTvLeft != null){
            mTvLeft.setVisibility(View.VISIBLE);
            mTvLeft.setText(name);
            mTvLeft.setOnClickListener(v -> finish());
            DrawableUtils drawableUtils = new DrawableUtils();
            mTvLeft.setCompoundDrawables(drawableUtils.getDrawable(R.mipmap.ic_back, 2), null, null, null);
        }
    }

    @Override
    public void setIvLeft(int image) {

    }

    @Override
    public void setTvRight(String name) {
        if("".equals(name)){
            mTvRight.setVisibility(View.GONE);
            return;
        }
        if(mTvRight != null){
            mTvRight.setVisibility(View.VISIBLE);
            if(name.length() > 2){
                SpannableString spannableString = new SpannableString(name);
                RelativeSizeSpan sizeSpan01 = new RelativeSizeSpan(0.8f);
                spannableString.setSpan(sizeSpan01, 0, name.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                mTvRight.setText(spannableString);
            }else{
                mTvRight.setText(name);
            }
        }
    }

    @Override
    public void setIvRight(int image) {
        if(mIvRight != null){
            mIvRight.setVisibility(View.VISIBLE);
            mIvRight.setImageResource(image);
        }
    }

    @Override
    public void setIvRight2(int image) {
        if(mIvRight2 != null){
            mIvRight2.setVisibility(View.VISIBLE);
            mIvRight2.setImageResource(image);
        }
    }

}
